<?php

session_start();

// Include i file necessari
include('inc/config.inc.php');
include('inc/template.inc.php');
include('inc/db.inc.php');
include('inc/check.inc.php');


// Recupera l'id della locazione
$loc_id = ( ! empty($_GET['loc'])) ? trim(mysql_real_escape_string($_GET['loc'])) : 1;

$uid = $_SESSION['uid'];
$unm = $_SESSION['unm'];

// Registra l'attuale locazione del personaggio.
$db -> query("UPDATE users SET user_loc = $loc_id WHERE user_id = '$uid'");

// Controllo che ci sia il cambio della password più vecchia di 6 mesi.
$changepass = ( $_SESSION['changepass'] == 1 ) ? '<b>Cambia la <i>password</i></b>' : '';

// Controlla se sono presenti nuovi messaggi
if( $r = $db -> query("SELECT mail_id FROM mail WHERE mail_to = $uid and mail_read = 0") )
{
	$mail_count = $r -> num_rows;
		$r -> close();
}
	
// Recupera le informazioni della locazione
$query3 = "SELECT loc_name, loc_desc, loc_img, loc_file FROM locations WHERE loc_id = '$loc_id'";
if($r = $db -> query($query3) )
{
	$rd = $r -> fetch_assoc();

	// Variabili
	$loc_name = $rd['loc_name'];
	$loc_img = $rd['loc_img'];
	$loc_desc = $rd['loc_desc'];
	$loc_file = $rd['loc_file'] . "?loc=" . $loc_id;

	if ($loc_img == '')
	{
		$loc_img = 'img/locations/default.jpg';
	}
	// Inizio Menu
	$menu = '';
	$menu .= '<li class="tip"><a href="map.php" target="main" title="Mappa"><img src="img/navbar-buttons/mappa.png" /></a></li>';

	$menu .= '<li class="dropdown tip">';
	$menu .= '<a href="#" class="dropdown-toggle" data-toggle="dropdown" data-placement="top" title="Documenti"><img src="img/navbar-buttons/wiki.png" /></a>';
	$menu .= '<ul class="dropdown-menu">';
	$menu .= '<li><a href="lof.php" target="main">Lands of Fate</a></li>';
	$menu .= '<li><a href="regolamento.php" target="main">Regolamento</a></li>';
	$menu .= '<li><a href="staff.php" target="main">Staff</a></li>';
	$menu .= '<li class="divider"></li>';
	$menu .= '<li><a href="guilds.php" target="main">Mestieri</a></li>';
	$menu .= '<li><a href="guild.php" target="main">Gilde</a></li>';
	$menu .= '<li><a href="races.php" target="main">Razze</a></li>';
	$menu .= '<li><a href="guilds.php" target="main">Casate</a></li>';
	$menu .= '<li><a href="guilds.php" target="main">Clan</a></li>';
	$menu .= '</ul>';
	$menu .= '</li>';
	
	$menu .= '<li class="dropdown tip">';
	$menu .= '<a href="#" class="dropdown-toggle" data-toggle="dropdown" data-placement="top" title="Utilit&agrave;"><img src="img/navbar-buttons/utilita.png" /></a>';
	$menu .= '<ul class="dropdown-menu">';
	$menu .= '<li><a href="office.php" target="main">Anagrafe</a></li>';
	$menu .= '<li><a href="bank.php" target="main">Banca</a></li>';
	$menu .= '<li><a href="market.php" target="main">Mercato</a></li>';
	$menu .= '<li><a href="utils/guildrequest.php" target="main">Richiedi l\'ammissione a una gilda</a></li>';
	$menu .= '</ul>';
	$menu .= '</li>';
	
	if($mail_count == 0 ) 
	{
		$menu .= '<li class="tip"><a href="mail.php" target="main" id="mailcheck" title="Nessuna Missiva"><img src="img/navbar-buttons/missiva_no.png" /></a></li>';
	} else {
		$menu .= '<li class="tip"><a href="mail.php" target="main" id="mailcheck" title="Hai ' . $mail_count . ' Missive"><img src="img/navbar-buttons/missiva_si.png" /></a></li>';
	}
	
	$menu .= '<li class="tip"><a href="boards.php" target="main" title="Bacheche"><img src="img/navbar-buttons/bacheche.png" /></a></li>';
	$menu .= '<li class="tip"><a href="avatar.php" target="main" title="Avatar"><img src="img/navbar-buttons/avatar.png" /></a></li>';
	
	if($_SESSION['admin'] == 1 OR $_SESSION['founder'] == 1 OR $_SESSION['master'] == 1 OR $_SESSION['guildmaster'] != 0)
	{
		$menu .= '<li class="dropdown tip">';
		$menu .= '<a href="#" class="dropdown-toggle" data-toggle="dropdown" title="Gestione"><img src="img/navbar-buttons/gestione.png" /></a>';
		$menu .= '<ul class="dropdown-menu">';
		
		// Menu Master
		if($_SESSION['master'] == 1)
		{
			$menu .= '<li><a href="admin/master.php" target="main">Strumenti Master</a></li>';
		} else {
			$menu .= "";
		}
	
		// Menu Master di Gilda
		if($_SESSION['guildmaster'] != 0)
		{
			$menu .= '<li><a href="admin/guildmaster.php" target="main">Gestione Gilda</a></li>';
		} else {
			$menu .= ""; 
		}
		
		// Menu Admin
		if($_SESSION['admin'] == 1)
		{
			$menu .= '<li><a href="admin/admin.php" target="main">Amministrazione</a></li>';
		} else {
			$menu .= "";
		}
		
		// Menu Founder
		if($_SESSION['founder'] == 1)
		{
			$menu .= '<li><a href="admin/founder.php" target="main">Gestione Engine</a><li>';
		} else {
			$menu .= "";
		}
		
		//$log_iframe = '';
		
	}
	$menu .= '</ul>';
	$menu .= '</li>';
	
	//$menu .= '<li><a href="online.php?loc=' . $loc_id . '" target="main">Online</a></li>';
	$menu .= '<li class="tip"><a href="logout.php" title="Log-Out"><img src="img/navbar-buttons/esci.png" /></a></li></ul>';
	
	$menu .= "</div>";

	// Fine Menu
	
	$calendar = "";
	$date = date('d/m/Y', time());
	$d = explode('/', $date);
	$calendar .= '<small id="calendar">Giorno ' . $d[0] . ' Mese ' . $d[1] . ' Anno ' . ((int)$d[2] - 714) . '</small>'; 
	
	//Attivazione Shoutbox
	//$shoutbox_engine = '<iframe name="shoutbox" id="shoutbox" class="col-md-12" style="height: 360px" src="shoutbox.php"></iframe>';
	
	// Lista delle tag speciali
	$tags = array(
            'temp_title' => $g_name, 'temp_menu' => $menu,
            'temp_changepass' => $changepass, 'temp_locname' => $loc_name, 'temp_locimg' => $loc_img,
            'temp_locdesc' => $loc_desc, 'temp_locfile' => $loc_file, 'temp_locid' => $loc_id, 'temp_calendar' => $calendar
					/*'temp_sbengine' => $shoutbox_engine, 'temp_quests' => $active_quests */
				 );

	// Carica il template e formatta le tag speciali
	$temp = new template('style/game.template.html');
	$temp -> replace($tags);

	// Visualizza l'output
	echo $temp -> show();

	$r -> close();
}

// Chiude la connessione al database
$db->close();

?>